package class02;

/**
 * @author Gong Cheng
 * @date 2021/2/25 3:36 下午
 **/
public class Code04_RingArray {
    public static class MyQueue{
        private int[] arr;
        private int pushi;
        private int polli;
        private int size;
        private final int limit;

        public MyQueue(int l){
            arr = new int[l];
            pushi = 0;
            polli = 0;
            size = 0;
            limit = l;
        }

        public void push(int value){
            if ( size == limit){
                throw new RuntimeException("栈满了，不能再加了");
            }
            size ++;
            arr[pushi] = value;
            pushi = nexIndex(pushi);
        }

        public int pop(){
            if(size == 0){
                throw new RuntimeException("栈空了");
            }
            size--;
            int ans = arr[polli];
            polli = nexIndex(polli);
            return ans;
        }

        public int nexIndex(int i){
            return i < limit -1 ? i+1:0;
        }
    }
}
